package com.hoopchina.util.update;

import java.io.IOException;
import java.io.InputStream;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import android.util.Xml;

public class UpdateDetailXML {

	private static final String TAG = "UpdateDetailXML";
	private static final String VERSION = "version";
	private static final String APKURL = "url";
	private static final String DATE = "updatetime";

	public VersionInfo getUpdateInfo(InputStream aInputStream) {
		VersionInfo updateInfo = null;
		XmlPullParser xmlPull = Xml.newPullParser();
		try {
			xmlPull.setInput(aInputStream, "UTF-8");
			int eventCode = xmlPull.getEventType();

			while (eventCode != XmlPullParser.END_DOCUMENT) {
				switch (eventCode) {
				case XmlPullParser.START_DOCUMENT: // 文档开始事件,可以做一些数据初始化处理
					Log.i(TAG, "START_DOCUMENT");
					updateInfo = new VersionInfo();
					break;
				case XmlPullParser.START_TAG:// 元素开始
					String name = xmlPull.getName();
					String content = xmlPull.nextText();
					Log.i(TAG, "xmlPull.getName()=" + name);
					Log.i(TAG, "xmlPull.nextText()=" + content);

					if (name.equalsIgnoreCase(VERSION)) {
						updateInfo.setVerName(content);
					} else if (name.equalsIgnoreCase(DATE)) {
						updateInfo.setVerDate(content);
					} else if (name.equalsIgnoreCase(APKURL)) {
						updateInfo.setApkURL(content);
					}
					break;
				case XmlPullParser.END_TAG: // 元素结束,
					Log.i(TAG, "END_TAG");
					break;
				}// switch end
				eventCode = xmlPull.next();// 进入到一下一个元素.
				Log.i(TAG, "next element");
			}// while end

		} catch (XmlPullParserException e) {
			Log.i(TAG, e.toString());
		} catch (IOException e) {
			Log.i(TAG, e.toString());
		}catch (Exception e){
		    Log.i(TAG,e.toString());
		}
		Log.i(TAG, "XmlParser() OUT");
		return updateInfo;
	}
}